<?php
namespace app\command;

use app\mainadmin\model\SettingsModel;
use app\member\model\AccountLogModel;
use app\shop\model\OrderModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Crontab  extends Command
{
    protected function configure(){
        $this->setName('Crontab')->setDescription("计划任务 Crontab ");
    }
    
    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/
    
        $this->sf_award($output);
    
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }
    
    //释放待返广告费和待返星券
    private function sf_award($output){
        //查询所有支付的订单
        $set = new SettingsModel();
        $AccountLogModel = new AccountLogModel();
        $ordermodel = new OrderModel();
        $getset = $set->getRows();
        $ggf_release_day = $getset['ggf_release_day'];
        $xq_release_day = $getset['xq_release_day'];
        
        // $ggf_release_day = 0;
        // $xq_release_day = 0;
        
        $time = time();
        $ggf_release = $getset['ggf_release'];
        $xq_release = $getset['xq_release'];
        if($ggf_release_day > $xq_release_day){
            $nowtime = time()-($xq_release_day*60*60*24);
        }else{
            $nowtime = time()-($ggf_release_day*60*60*24);
        }
        $ggf_time = time()-($ggf_release_day*60*60*24);
        $xq_time = time()-($xq_release_day*60*60*24);
        $allorder = Db::table('shop_order_info')
        ->field('order_id,give_integral,goods_amount,give_dbalance,user_id,pay_time,xsf_time,gsf_time,order_status,shipping_status,pay_status')
        ->where('order_type',0)
        ->where('pay_status',1)
        ->where('give_dbalance|give_integral','>','0')
        ->where('pay_time','<',$nowtime)
        ->select();
      
        foreach($allorder as $orderv){
            if($orderv['give_dbalance'] > 0 and $orderv['gsf_time'] < $ggf_time){
                //释放广告费
                $award = round($orderv['goods_amount']*$ggf_release*0.01,2);
                if($award > 0.01){
                    if($award > $orderv['give_dbalance']){
                        $award = $orderv['give_dbalance'];
                    }
                    //减订单中广告费，减钱包中广告费，加钱包中余额
                    Db::startTrans();//启动事务
                    try {
                        Db::table('shop_order_info')->where('order_id',$orderv['order_id'])->update([
                            'give_dbalance'=>round($orderv['give_dbalance']-$award,2),
                            'gsf_time'=>$time
                        ]);
                        unset($changedata);
                        $ordermodel->_log($orderv, '释放广告费到余额');
                        $changedata['change_desc'] = '释放广告费到余额';
                        $changedata['change_type'] = 2;
                        $changedata['by_id'] = $orderv['order_id'];
                        $changedata['bean_value'] = abs($award)* -1;
                        $changedata['balance_money'] = abs($award);
                        // var_dump($changedata);
                        $res = $AccountLogModel->change($changedata, $orderv['user_id'], false);
                        $inlog = [
                            'user_id'    =>$orderv['user_id'],
                            'sf_dbalance'=>abs($award),
                            'sf_integral'=>0,
                            'order_id'   =>$orderv['order_id'],
                            'createtime' =>$time
                        ];
                        $res2 = Db::table('sf_log')->insertGetId($inlog);
                        if ($res !== true or empty($res2)) {
                            Db::rollback();// 回滚事务
                            $output->writeln('give_dbalance-error:改变广告费和余额失败');
                        }else{
                            Db::commit();// 提交事务
                        }
                       
                    } catch (\Throwable $th) {
                        //throw $th;
                        Db::rollback();// 回滚事务
                        $output->writeln('give_dbalance-error:'.$orderv['order_id']);
                    }
                }
            }
            if($orderv['give_integral'] > 0 and $orderv['xsf_time'] < $xq_time){
                //释放待返星券
                $award = round($orderv['goods_amount']*$xq_release*0.01,2);
                if($award > 0.01){
                     if($award > $orderv['give_integral']){
                        $award = $orderv['give_integral'];
                    }
                    //减订单中待返星券，减钱包中待返星券，加钱包中星券
                    Db::startTrans();//启动事务
                    try {
                        Db::table('shop_order_info')->where('order_id',$orderv['order_id'])->update([
                            'give_integral'=>round($orderv['give_integral']-$award,2),
                            'xsf_time'=>$time
                        ]);
                        unset($changedata2);
                        $ordermodel->_log($orderv, '释放待返星券到星券');
                        $changedata2['change_desc'] = '释放待返星券到星券';
                        $changedata2['change_type'] = 2;
                        $changedata2['by_id'] = $orderv['order_id'];
                        $changedata2['d_use_integral'] = abs($award) * -1;
                        $changedata2['use_integral'] = abs($award);
                        // var_dump($changedata2);
                        $res = $AccountLogModel->change($changedata2, $orderv['user_id'], false);
                        unset($inlog);
                        $inlog = [
                            'user_id'    =>$orderv['user_id'],
                            'sf_dbalance'=>0,
                            'sf_integral'=>abs($award),
                            'order_id'   =>$orderv['order_id'],
                            'createtime' =>$time
                        ];
                        $res2 = Db::table('sf_log')->insertGetId($inlog);
                        if ($res !== true or empty($res2)) {
                            Db::rollback();// 回滚事务
                            $output->writeln('give_integral-error:改变待返星券和星券失败');
                        }else{
                            Db::commit();// 提交事务
                        }
                        
                    } catch (\Throwable $th) {
                        //throw $th;
                        Db::rollback();// 回滚事务
                        $output->writeln('give_integral-error:'.$orderv['order_id']);
                    }
                }
            }
        }
    }
}
    